package com.lie.flat.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.time.Duration;


public class IdempotentUtils {
    private IdempotentUtils() {
    }

    public static final Cache<String, Boolean> localCache = CacheBuilder.newBuilder().
            initialCapacity(200)
            .expireAfterWrite(Duration.ofSeconds(1))
            .build();

    public static final Cache<String, Boolean> localCachePhone = CacheBuilder.newBuilder().
            initialCapacity(200)
            .expireAfterWrite(Duration.ofSeconds(30))
            .build();

    /**
     * 重复请求判断
     *
     * @param id
     * @return
     */
    public static boolean judge(Cache<String, Boolean> cache, String id) {
        if (cache.getIfPresent(id) != null) {
            // 重复请求
            return false;
        }
        // 非重复请求，存储请求 ID
        cache.put(id, true);
        return true;
    }

}
